#!/usr/bin/env python3

import os

def main():
    mes = os.pipe()

    if os.fork() == 0:
        os.close(mes[0])
        os.close(2)
        os.dup2(mes[1], 1)
        os.execlp("mplayer", "mplayer", "fa.wmv", "-input", "file=kyo", "-quiet", "-slave")

    os.close(mes[1])
    ctl = os.open("./kyo", os.O_RDWR)

    while True:
        print("1. 播放/暂停")
        print("2. 获取视频时长")
        print("3. 获取当前位置")
        print("4. 退出")
        s = input("请输入[1 - 4]: ")
        if not s:
            continue

        s = int(s)

        if s == 1:
            os.write(ctl, b"p\n")
        elif s == 2:
            os.write(ctl, b"get_time_length\n")
            print(os.read(mes[0], 1024*1024))
        elif s == 3:
            os.write(ctl, b"get_time_pos\n")
            print(os.read(mes[0], 1024*1024))
        elif s == 4:
            os.write(ctl, b"q\n")
            break

    os.wait()

if __name__ == "__main__":
    main()
